var util = require('../../utils/util.js')
var app = getApp()
var serverUrl = 'http://skyforest.static.elab-plus.com/wepy_pro/'
var {authorizeInfo,getUserInfo} = require('../../getlogininfo.js')
Page({
    data:{
        serverUrl:serverUrl,
        count:'',
        list:[],
        pageNo:1,//初始页码
        total:'',//总页数后台获取
        showInfoModel:false,
        showPhoneModel:false,
        phoneFailFun:null,
        phoneFun:null,
        hasNotice:false
    },
    onShow: function(e){
        wx.setNavigationBarTitle({
            title: '消息通知'
        })
        var obj={
            "pageid":'10941013',
            "keyvalue":'sfc.nb.xcx.w3.head.xxtz.enter',
        }
        util.reqTrackEventObj(obj,app);
        var obj={
            "pageid":'10941013',
            "keyvalue":'sfc.nb.xcx.w3.head.xxtz.statetime',
            "usetime":"3000", // 使用时长
        }
        util.reqTrackEventTimeObj(obj,app);
    },
    onUnload(){
        util.stopTrackEventTimeObj();
    },
    onHide(){
        util.stopTrackEventTimeObj();
    },
    getList:function(){
        var that=this;
        if(that.data.total&&that.data.pageNo>that.data.total){//+1后的pageNo大于total结束
            wx.showToast({
                title: "无更多消息",
                icon: 'warn',
                duration: 1500,
            })
            return false;
        }
        console.log(app.globalData.single.id,111111111111);
        var parm={pageNo:that.data.pageNo,customerId:app.globalData.single.id};//页码和customerId
        // var parm={pageNo:that.data.pageNo,customerId:"20045"};//联调数据
        wx.request({
            url: util.url(),
            method: 'POST',
            data: util.reformParam(util.getMessageList, parm),
            header: {
                'content-type': 'application/json' // 默认值
            },
            success:function(res){
                console.log('解密后 data: ', res);
                if(res.data.success&&res.data.pageModel.resultSet.length>0){
                    that.setData({
                        "list":that.data.list.concat(res.data.pageModel.resultSet),
                        "total":res.data.pageModel.total,
                        "pageNo":that.data.pageNo+1,//页码加1
                        "hasNotice":true
                    })
                }else if(!res.data.success){
                    wx.showToast({
                        title: res.data.message,
                        icon: 'warn',
                        duration: 1500,
                    })

                }
            },
            fail: function (res) {
                console.log('获取信息失败: ', res);
                if (res.errMsg == 'request:fail timeout') {
                    wx.showToast({
                        title: "网络请求超时，请检查网络状态",
                        icon: 'warn',
                        duration: 1500,
                    })
                }
                else{
                    wx.showToast({
                        title: res.errMsg,
                        icon: 'warn',
                        duration: 1500,
                    })
                }
            }
        });
    },
    onLoad:function(options){
        var that=this;
        authorizeInfo.call(this,function(){
            that.authorizeIndexPhone(function () {
                that.getList();
            },function () {
                wx.navigateBack({ delta: 1 });
            })
        },function(){
            wx.navigateBack({ delta: 1 });
        });
    },//手机号授权
    authorizeIndexPhone(cb,failcb){
        var that = this;
        // wx.getStorageSync('ISauthorizePhone');// 是否授权过手机号,
        if(!wx.getStorageSync('phone')){
            app.globalData.phone = wx.getStorageSync('phone');
            console.log("***authorizeIndexPhone***",app.globalData.phone,wx.getStorageSync('phone'))
            // 没有获得用户手机号
            if(!app.globalData.phone){
                that.setData({
                    showPhoneModel: true,
                    phoneFun:cb||null,
                    phoneFailFun:failcb||null
                })
            }
            else{
                that.setData({
                    showPhoneModel: false,
                    showPhoneAuth:true
                })
                typeof cb == "function" && cb()
            }
        }else{
            typeof cb == "function" && cb()
        }
    },
    // 手机号授权
    getPhoneNumber: function (e) {
        var self = this;
        // 隐藏
        this.setData({
            showPhoneModel: false
        })
        wx.setStorageSync('ISauthorizePhone',true);// 是否授权过手机号,
        var iv = encodeURIComponent(encodeURIComponent(e.detail.iv));
        var errMsg = e.detail.errMsg;
        var houseid=app.globalData.houseid;
        var token=app.globalData.tonken||"";
        var encryData = encodeURIComponent(encodeURIComponent(e.detail.encryptedData));
        var session = encodeURIComponent(encodeURIComponent(app.globalData.sessionKey));
        var appid = encodeURIComponent(encodeURIComponent(app.globalData.appid));//"wxa6aef323462b8b14";
        console.log("***token***",token)
        // var pc = new WXBizDataCrypt(appid, session);
        console.log("****getPhoneNumber****")
        // 用户未授权
        if (e.detail.errMsg.includes("fail")){
            typeof self.data.phoneFailFun == "function" && self.data.phoneFailFun();
        }
        else{
            var parm={
                encryptedData:encryData,
                sessionKey:session,
                appId:appid,
                houseid:houseid,
                iv:iv
            };
            console.log(2222222,parm);
            wx.request({
                url: util.url(),
                method: 'POST',
                data: util.reformParam(util.getPhoneNumber, parm),
                header: {
                    'content-type': 'application/json', // 默认值
                    'auth_token':token
                },
                success:function(res){
                    console.log('解密后 data: ', res);
                    if(res.data.success && res.data.single){
                        console.log(res.data.single)
                        app.globalData.phone = res.data.single;
                        wx.setStorageSync('phone', res.data.single);
                        self.setData({
                            showPhoneModel: false,
                            showPhoneAuth:true,
                        })
                        // 存在跳转的
                        typeof self.data.phoneFun == "function" && self.data.phoneFun(); // 执行回调函数
                    }
                },
                fail:function(res){
                    typeof self.data.phoneFailFun == "function" && self.data.phoneFailFun();
                    console.log(res,'解密手机号失败ggggggggggg')
                }
            });
        }
    },
    // 用户授权
    getUserInfo: function (e) {
        getUserInfo.call(this,e);
    },
    onReachBottom: function() {
        // Do something when pull down.
        this.getList();
    },
    goToDetail:function (e) {
        var url=e.target.dataset.pageparams&&JSON.parse(e.target.dataset.pageparams).url;
        var messageId=e.target.dataset.messageid;
        var readStatus=e.target.dataset.readstatus;
        if(!readStatus){//未读状态发请求
            var parm={messageId:messageId};//messageId
            wx.request({
                url: util.url(),
                method: 'POST',
                data: util.reformParam(util.markRead, parm),
                header: {
                    'content-type': 'application/json' // 默认值
                },
                success:function(res){
                    console.log('解密后 data: ', res);
                }
            });
        }
        if(url){//消息的pageParams里有url跳h5
            var view=encodeURIComponent(url);
            wx.navigateTo({
                url: '../webView/webView?view='+view
            })
        }
    }
})
